﻿@using Partner.Surround.Web.Startup
@using Partner.Surround.Authorization
@{
    ViewBag.CurrentPageName = PageNames.Editions;
}

<!--版本列表-->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-btn-container">
                @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Frame_Editions_Create))
                {
                    <button class="layui-btn layui-btn-edition" data-type="add">添加</button>
                }
            </div>
            <div class="layui-row">
                <div class="layui-col-xs12">
                    <table class="layui-table"
                           lay-data="{height: 'full-120', page:false, id:'editionList'}"
                           lay-filter="editionList">
                        <thead>
                            <tr>
                                <th lay-data="{field:'displayName'}">版本名称</th>
                                <th lay-data="{field:'lastModificationTime',templet:'#lastModificationTime'}">最后修改时间</th>
                                @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Frame_Editions_Update))
                                {
                                    <th lay-data="{align:'center',toolbar:'#editionBarList'}"></th>
                                }
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!--列表按钮-->
<script type="text/html" id="editionBarList">
    <div class="layui-table-cell laytable-cell-1-0-9">
        @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Frame_Roles_Update))
        {
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        }
        @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Frame_Roles_Delete))
        {
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        }
    </div>
</script>

@await Html.PartialAsync("_LastModificationTime")

@section Scripts{
    <script type="text/javascript">
        layui.config({
            base: "/lib/layui-extend-modules/"
        }).use(['jquery', 'table'], function () {
            var $ = layui.$,
                table = layui.table;

            //加载表格数据
            var loadEditionList = function () {
                table.reload('editionList', {
                    url: "@Url.Action("GetEditionsList", "Editions")",
                    parseData: function (res) {
                        return {
                            "code": res.result.code,
                            "msg": res.result.msg,
                            "count": res.result.count,
                            "data": res.result.data
                        };
                    },
                    response: {
                        statusCode: 200
                    }
                });
            };
            loadEditionList();

            //删除版本
            function deleteEdition(id) {
                parent.layer.confirm("确认删除?", function (index) {
                    abp.ajax({
                        url: "@Url.Action("DeleteEdition", "Editions")",
                        data: JSON.stringify(id),
                        abpHandleError: false
                    }).done(function (data) {
                        if (data.code == 200) {
                            abp.notify.success(data.msg);
                            loadEditionList();//重新刷新数据
                        }
                    }).fail(function (jqXHR) {
                        parent.layer.msg(jqXHR.message, { icon: 5 });
                    }).always(function () {
                        parent.layer.close(index);
                    });
                });
            }

            //监听工具条
            table.on('tool(editionList)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {   //删除
                    deleteEdition({ "id": data.id });
                } else if (obj.event === 'edit') {  //编辑
                    var editionData = {
                        title: '编辑版本信息',
                        content: "@Url.Action("CreateOrUpdateEdition", "Editions")" + abp.utils.formatString("?id={0}", data.id)
                    };
                    createOrUpdateEdition(editionData);
                }
            });

            //创建或更新版本
            function createOrUpdateEdition(editionData) {
                parent.layer.open({
                    type: 2,
                    title: editionData.title,
                    content: editionData.content,
                    area: ["650px", "320px"],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].layui.submitForm();
                    }
                });
            }

            //创建或更新完毕回调
            layui.callback = function (data) {
                parent.layer.close(data.index); //关闭弹层
                abp.notify.success(data.msg);
                loadEditionList();
            }

            //监听事件处理
            var active = {
                add: function () {
                    var editionData = {
                        title: '添加版本',
                        content:"@Url.Action("CreateOrUpdateEdition", "Editions")"
                    };
                    createOrUpdateEdition(editionData);
                }
            };

            //点击事件转发器
            $('.layui-btn.layui-btn-edition').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
}